home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15292 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  46 lines

  1. Newsgroups: comp.lang.c,comp.unix.programmer
  2. Path: phcoms4.seri.philips.nl!panther!baynes
  3. From: baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes)
  4. Subject: fgetc and getc (was Q: '\n' character)
  5. Sender: news@ukpsshp1.serigate.philips.nl (account for localnews)
  6. Message-ID: <Dq1s7F.Bo1@ukpsshp1.serigate.philips.nl>
  7. Date: Thu, 18 Apr 1996 07:45:15 GMT
  8. References: <4kj66f$k0o@ren.cei.net> <4ku8f9$d3o@mark.ucdavis.edu> <4kumbqINNgcr@mayne.ugrad.cs.ubc.ca> <4l13uu$mva@mark.ucdavis.edu> <4l1ap4INNhsc@keats.ugrad.cs.ubc.ca>
  9. Organization: Philips Semiconductors, Southampton, UK
  10. X-Newsreader: TIN [version 1.2 PL2]
  11. Followup-To: comp.lang.c,comp.unix.programmer
  12.  
  13. Kazimir Kylheku (c2a192@ugrad.cs.ubc.ca) wrote:
  14. : Also, you used fputc() and fgetc(). These are not likely to be as fast as the
  15. : getc() and putc() macro equivalents which access the FILE structure's buffer
  16. : directly. (Do you know the implementation details of these functions/macros on
  17. : the system that served as your testbench?)
  18.  
  19. As far as ANSI C is concerned they are all functions, and any library function
  20. can also have a macro implementation hiding it. It is a convention of
  21. unix implementations that getc is a macro and fgetc is a function. As far
  22. as I am aware the unix standards (POSIX and XOPEN) just require the C compiler
  23. to be ANSI so there is no guarentee that you will a macro for getc.
  24. Note however ANSI C does relax the rules slightly for getc and putc, if they
  25. are implemented as a macro they can evaluate their argument more than once,
  26. which fgetc and fputc cannot. This might make macro implementations of getc
  27. and putc more efficient than ones of fgetc and fputc.
  28. You also make the assumption that macros must be faster than functions. 
  29. It is often true that they are, but they don't have to be. For example a 
  30. function hand written in assembler may be faster than anything the C compiler 
  31. can do with a macro. In fact an implementor would not bother with providing
  32. a macro for getc if a function would do it faster.
  33.  
  34. The end result of your reasoning is correct, getc and putc will normally be no 
  35. slower than fgetc and fputc and probably faster, but the reason is because 
  36. the implementor can exploit weaker rules on argument evaluation. However there
  37. are plenty of pathologies, for example if the arguments are complex then the
  38. extra evaluation could cost more.  
  39.  
  40. --
  41. Stephen Baynes                              baynes@ukpsshp1.serigate.philips.nl
  42. Philips Semiconductors Ltd
  43. Southampton                                 My views are my own.
  44. United Kingdom
  45.  Are you using ISO8859-1? Do you see ⌐ as copyright, ≈ as division and ╜ as 1/2?
  46.